//
//  XNetworkManager+FlowerExtension.swift
//  WeiBoReDo
//
//  Created by 放学跟鸽走 on 2017/4/18.
//  Copyright © 2017年 放学跟鸽走. All rights reserved.
//

import Foundation
// MARK: - 花各种列表请求数据基类
fileprivate extension XNetworkManager{
    func baseFlowerListRequest(url:String,params:[String:Any],complete:@escaping (_ page: Int,_ list:[[String:Any]]?,_ isSuccess:Bool)->()){
        request(urlString: url, params: params) { (json, isSuccess) in
            if isSuccess{
                
                guard  let dic = (json as? [String:Any]),//转成字典
                    let array:[[String:Any]] = dic["dataList"]  as? [[String:Any]],//取出reslut对应的字典数组
                    let pageDic:[String:Any] = dic["page"] as? [String:Any],//取出页码
                    
                    let cpage = Int((pageDic["p"] as? String) ?? "1")
                    else{
                        //以上步骤出现错误，直接返回
                        complete(1,nil,false)
                        return
                }
                //得到数组，返回
                complete(cpage,array,true)
            }else{
                //请求失败，返回
                complete(1,nil,false)
            }
        }
    }
}

extension XNetworkManager{
    func getFlowerList(page:Int,complete:@escaping (_ page: Int,_ list:[[String:Any]]?,_ isSuccess:Bool)->()){
        
        let url = "http://app.wodinghua.com/api/product/index"
        let params = ["ot":0,"p":page]
        
        baseFlowerListRequest(url: url, params: params, complete: complete)

        
    }
    
}
